package com.rainxing.cockpit.utils;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: ybzhu
 * @Date: 2018/11/27 15:23
 * @Mail: 110668032@qq.com
 * @Description:
 */
public class TbAndHbTools {

    /**
     * 同比、环比 默认
     *
     * @param strList
     * @param endList
     * @return
     */
    public static Map<String, Object> getAllCount(List<Map<String, Object>> strList, List<Map<String, Object>> endList) {
        Map<String, Object> map = new HashMap<>();
        for (int i = 0; i < strList.size(); i++) {
            Map<String, Object> str = strList.get(i);
            float strCount = Float.parseFloat(str.get("counts").toString());
            for (int j = 0; j < endList.size(); j++) {
                Map<String, Object> end = endList.get(j);
                float endCount = Float.parseFloat(end.get("counts").toString());
                String accuracy = accuracy(strCount - endCount, endCount, 2);
                map.put("a" + (i + 1), accuracy);
                map.put("b" + (i + 1), strCount - endCount);
            }
        }
        return map;
    }

    /**
     * 同比、环比 街道下各执法队排名
     *
     * @param strList
     * @param endList
     * @return
     */
    public static Map<String, Object> getRankCount(List<Map<String, Object>> strList, List<Map<String, Object>> endList) {
        Map<String, Object> map = new HashMap<>();
        for (int i = 0; i < strList.size(); i++) {
            Map<String, Object> str = strList.get(i);
            Integer strCount = Integer.valueOf(str.get("counts").toString());
            String strName = String.valueOf(str.get("DepartmentName"));
            for (int j = 0; j < endList.size(); j++) {
                Map<String, Object> end = endList.get(j);
                Integer endCount = Integer.valueOf(end.get("counts").toString());
                String endName = String.valueOf(end.get("DepartmentName"));
                if (strName.equals(endName)) {
                    String accuracy = accuracy(strCount - endCount, endCount, 2);
                    map.put(DepartmentTools.newTooldName(strName), accuracy);
                }
            }
        }
        return map;
    }

    /**
     * 同比 按照月份
     *
     * @param strList
     * @param endList
     * @return
     */
    public static Map<String, Object> getMonthsCount(List<Map<String, Object>> strList, List<Map<String, Object>> endList) {
        Map<String, Object> map = new HashMap<>();
        for (int i = 0; i < strList.size(); i++) {
            Map<String, Object> str = strList.get(i);
            float strCount = Float.parseFloat(str.get("counts").toString());
            String strMonths = str.get("months").toString();
            for (int j = 0; j < endList.size(); j++) {
                Map<String, Object> end = endList.get(j);
                String endMonths = end.get("months").toString();
                int strMonth = Integer.valueOf(strMonths.substring(5, 7));
                int endMonth = Integer.valueOf(endMonths.substring(5, 7));
                if (strMonth != 0 && strMonth == endMonth) {
                    float endCount = Float.parseFloat(end.get("counts").toString());
                    String accuracy = accuracy(strCount - endCount, endCount, 2);
                    map.put(strMonths + "", accuracy);
                }
            }
        }
        return map;
    }

    /**
     * 环比 按照月份
     *
     * @param strList
     * @param endList
     * @return
     */
    public static Map<String, Object> getHBMonthsCount(List<Map<String, Object>> strList, List<Map<String, Object>> endList) {
        Map<String, Object> map = new HashMap<>();
        for (int i = 0; i < strList.size(); i++) {
            Map<String, Object> str = strList.get(i);
            float strCount = Float.parseFloat(str.get("counts").toString());
            String strMonths = str.get("months").toString();
            for (int j = 0; j < endList.size(); j++) {
                Map<String, Object> end = endList.get(j);
                String endMonths = end.get("months").toString();
                int strMonth = Integer.valueOf(strMonths.substring(5, 7)) - 1;
                int endMonth = Integer.valueOf(endMonths.substring(5, 7));
                if (strMonth != 0 && strMonth == endMonth) {
                    float endCount = Float.parseFloat(end.get("counts").toString());
                    String accuracy = accuracy(strCount - endCount, endCount, 2);
                    map.put(strMonths + "", accuracy);
                }
            }
        }
        return map;
    }

    /**
     * 环比 按照月份
     *
     * @param strList
     * @param endList
     * @return
     */
    public static Map<String, Object> getHBWeekCount(List<Map<String, Object>> strList, List<Map<String, Object>> endList) {
        Map<String, Object> map = new HashMap<>();
        for (int i = 0; i < strList.size(); i++) {
            Map<String, Object> str = strList.get(i);
            Integer strCount = Integer.valueOf(str.get("counts").toString());
            String strName = str.get("DepartmentName").toString();
            for (int j = 0; j < endList.size(); j++) {
                Map<String, Object> end = endList.get(j);
                Integer endCount = Integer.valueOf(end.get("counts").toString());
                String endName = String.valueOf(end.get("DepartmentName"));
                if (strName.equals(endName)) {
                    String accuracy = "";
                    if (strCount != 0 && endCount == 0) {
                        accuracy = strCount + "00%";
                        map.put(DepartmentTools.newTooldName(strName) + "-" + (i + 1), accuracy);
                        map.put("name-" + (i + 1), strCount - endCount);
                    } else if (strCount == 0 && endCount != 0) {
                        accuracy = "-" + endCount + "00%";
                        map.put(DepartmentTools.newTooldName(strName) + "-" + (i + 1), accuracy);
                        map.put("name-" + (i + 1), strCount - endCount);
                    } else if (strCount == 0 && endCount == 0) {
                        accuracy = "0%";
                        map.put(DepartmentTools.newTooldName(strName) + "-" + (i + 1), accuracy);
                        map.put("name-" + (i + 1), 0);
                    } else {
                        accuracy = accuracy(strCount - endCount, endCount, 2);
                        map.put(DepartmentTools.newTooldName(strName) + "-" + (i + 1), accuracy);
                        map.put("name-" + (i + 1), strCount - endCount);
                    }
                    break;
                }
            }
        }
        return map;
    }

    /**
     * 两个 double 求百分比
     *
     * @param num
     * @param total
     * @param scale
     * @return
     */
    public static String accuracy(double num, double total, int scale) {
        DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
        //可以设置精确几位小数
        df.setMaximumFractionDigits(scale);
        //模式 例如四舍五入
        df.setRoundingMode(RoundingMode.HALF_UP);
        double accuracy_num = num / total * 100;
        return df.format(accuracy_num) + "%";
    }

    public static void main(String[] args) {
        String str = "2018-08";
        String substring = str.substring(5, 7);
        System.out.println(substring);
    }
}
